package keter.ksf.util;

import com.jfinal.kit.PathKit;
import keter.ksf.model.Mapping;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by gu on 2017/3/3.
 */
public class MappingUtil {

    private static final Logger logger = LoggerFactory.getLogger(MappingUtil.class);

    /**
     * 获取口味码映射表
     * @param f
     * @return
     * @throws IOException
     */
    public static Mapping getMapping(File f, int sheetIdx) throws IOException {
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(f));
        XSSFSheet st = wb.getSheetAt(sheetIdx);//文档名->口味码,描述
        Mapping m = new Mapping();
        for (int i = 2; i <= st.getLastRowNum(); i++) {
            m.put(getCellStr(st.getRow(i).getCell(3)),
                    getCellStr(st.getRow(i).getCell(1))+ "," + getCellStr(st.getRow(i).getCell(2)));
        }
        return m;
    }

    private static String getCellStr(Cell cell){
        DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
        return formatter.formatCellValue(cell);
    }

    public static void main(String[] args) throws IOException {
        File mappingFile = new File(PathKit.getRootClassPath()+"/doc/mapping/mappings.xlsx");
        Mapping m = MappingUtil.getMapping(mappingFile,0);
        System.out.println(m.getFirstValue("冰糖系-辽北PET1000冰糖雪梨"));
        System.out.println(m.getSecondValue("冰糖系-辽北PET1000冰糖雪梨"));

        m = MappingUtil.getMapping(mappingFile,1);
        System.out.println(m.getFirstValue("锦州"));
        System.out.println(m.getSecondValue("朝阳"));
    }
}
